package com.xebest.web.controller.homepage;

import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.web.controller.order.XeVenOrderController;
import com.xebest.web.service.XeVenOrderService;
import com.xebest.web.view.ErpUserInfo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 店铺概览相关
 *
 * @author 李书峰
 */

@RestController
@RequestMapping("shop")
public class XeVenShopPageController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(XeVenOrderController.class);//日志记录类

    @Autowired
    private XeVenOrderService xeVenOrderService;//订单相关业务操作

    /**
     * 店铺概览
     *
     * @author 李书峰
     */
    @RequestMapping("page")
    @ResponseBody
    public BaseResponse shopPage() {
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("userCode", userInfo.getUserCode());
            // 3、输出结果
            BigDecimal waitCheckOrderNum = xeVenOrderService.queryWaitCheckOrderNum(map);
            waitCheckOrderNum = null != waitCheckOrderNum ? waitCheckOrderNum : new BigDecimal(0);

            BigDecimal waitSendOrderNum = xeVenOrderService.queryWaitSendOrderNum(map);
            waitSendOrderNum = null != waitSendOrderNum ? waitSendOrderNum : new BigDecimal(0);

            map = xeVenOrderService.queryShopYesterdayData(map);
            BigDecimal yesterdayTradeUsersNum = null != map.get("yesterdayTradeUsersNum")
                    && StringUtils.isNotBlank(map.get("yesterdayTradeUsersNum").toString())
                    ? new BigDecimal(map.get("yesterdayTradeUsersNum").toString())
                    : new BigDecimal(0);
            BigDecimal yesterdayOrderNum = null != map.get("yesterdayOrderNum")
                    && StringUtils.isNotBlank(map.get("yesterdayOrderNum").toString())
                    ? new BigDecimal(map.get("yesterdayOrderNum").toString())
                    : new BigDecimal(0);
            BigDecimal yesterdayTradeAmount = null != map.get("yesterdayTradeAmount")
                    && StringUtils.isNotBlank(map.get("yesterdayTradeAmount").toString())
                    ? new BigDecimal(map.get("yesterdayTradeAmount").toString())
                    : new BigDecimal(0);

            map.clear();

            map.put("waitCheckOrderNum", waitCheckOrderNum);
            map.put("waitSendOrderNum", waitSendOrderNum);
            map.put("yesterdayTradeUsersNum", yesterdayTradeUsersNum);
            map.put("yesterdayOrderNum", yesterdayOrderNum);
            map.put("yesterdayTradeAmount", yesterdayTradeAmount);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询店铺概览数据异常：{}", e);
            return sendAjaxError("查询店铺概览数据异常");
        } finally {
            log.info("--------------查询店铺概览数据end-------------------");
        }
    }
}
  
